﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressAdminBusinessLayer;
using AlexandriaPressUserBusinessLayer;

namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm15 : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.Name != "")
            {
                UserBusinsessLayer business = new UserBusinsessLayer();
                string _UserType = business.getUserType(HttpContext.Current.User.Identity.Name);
                if (_UserType != "Type_Admin")
                {
                    Response.Redirect("~/MasterPages/ErrorPage.aspx");
                }
            } 
        }




        static int month;
        static int year;
        static bool titleSelected;
        bool buttonClick;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.Visible = false;
                DropDownList2.Visible = false;
                Label1.Visible = false;


            }

            if(buttonClick == true)
            {
            gridSalesByTitle.Visible = false;
            gridSalesByGenre.EmptyDataText = "No data available";
            gridSalesByGenre.DataSource = null;
            gridSalesByGenre.DataBind();
            gridSalesByGenre.Visible = true;
            }
            else
            {
                gridSalesByGenre.Visible = false;
                gridSalesByTitle.EmptyDataText = "No data available";
                gridSalesByTitle.DataSource = null;
                gridSalesByTitle.DataBind();
                gridSalesByTitle.Visible = true;
            }
     


        }

        private void visibility()
        {
            DropDownList1.SelectedIndex = 0;
            DropDownList2.SelectedIndex = 0;
            DropDownList1.Visible = true;
            DropDownList2.Visible = true;
            Label1.Visible = true;
        }

        protected void btnSalesGenre_Click(object sender, EventArgs e)
        {
            buttonClick = false;


            titleSelected = false;
            visibility();            
        }

        protected void btnSalesBook_Click(object sender, EventArgs e)
        {
            buttonClick = true;


            titleSelected = true;
            visibility();            
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (DropDownList2.SelectedIndex != 0 && DropDownList1.SelectedIndex != 0)
            {
                month = Convert.ToInt32(DropDownList1.SelectedValue);
                year = Convert.ToInt32(DropDownList2.SelectedValue);
                if (titleSelected == false)
                {
                    gridSalesByGenre.Visible = true;
                    gridSalesByTitle.Visible = false;
                    gridSalesByGenre.DataSource = SalesReportBL.salesByGenre(month, year);
                    gridSalesByGenre.EmptyDataText = "No data available";
                    gridSalesByGenre.DataBind();
                }
                else if (titleSelected == true)
                {
                    gridSalesByTitle.Visible = true;
                    gridSalesByGenre.Visible = false;
                    gridSalesByTitle.DataSource = SalesReportBL.salesByBook(month, year);
                    gridSalesByTitle.EmptyDataText = "No data available";
                    gridSalesByTitle.DataBind();
                }

            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (DropDownList1.SelectedIndex != 0 && DropDownList2.SelectedIndex != 0)
            {
                month = Convert.ToInt32(DropDownList1.SelectedValue);
                year = Convert.ToInt32(DropDownList2.SelectedValue);
                if (titleSelected == false)
                {
                    gridSalesByGenre.Visible = true;
                    gridSalesByTitle.Visible = false;
                    gridSalesByGenre.DataSource = SalesReportBL.salesByGenre(month,year);
                    gridSalesByGenre.EmptyDataText = "No data available";
                    gridSalesByGenre.DataBind();
                }
                else if (titleSelected == true)
                {
                    gridSalesByTitle.Visible = true;
                    gridSalesByGenre.Visible = false;
                    gridSalesByTitle.DataSource = SalesReportBL.salesByBook(month, year);
                    gridSalesByTitle.EmptyDataText = "No data available";
                    gridSalesByTitle.DataBind();
                }

            }
        }

        protected void gridSalesByGenre_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridSalesByGenre.Visible = true;
   

            gridSalesByGenre.PageIndex = e.NewPageIndex;
            gridSalesByGenre.DataSource = SalesReportBL.salesByGenre(month, year);
            gridSalesByGenre.DataBind();
        }

        protected void gridSalesByTitle_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridSalesByTitle.Visible = true;


            gridSalesByTitle.PageIndex = e.NewPageIndex;
            gridSalesByTitle.DataSource = SalesReportBL.salesByBook(month, year);
            gridSalesByTitle.DataBind();
        }




        

        


    }
}
